package com.niiwoo.civet.account.service.local.fundsFreeze;

import com.niiwoo.civet.account.dao.entity.AccountBase;
import com.niiwoo.civet.account.dao.entity.FundsUnfreezeOrder;
import com.niiwoo.civet.account.dao.mapper.AccountBaseMapperExt;
import com.niiwoo.civet.account.dao.mapper.FundsFreezeOrderMapperExt;
import com.niiwoo.civet.account.dao.mapper.FundsUnfreezeOrderMapperExt;
import com.niiwoo.civet.account.dto.request.FundsUnfreezeOrderApplyRequestDTO;
import com.niiwoo.civet.account.enums.FundsFreezeOrderStatusEnum;
import com.niiwoo.civet.account.service.local.AccountService;
import com.niiwoo.tripod.lanmao.component.LanMaoSequence;
import com.niiwoo.tripod.provider.exception.BizException;
import com.niiwoo.tripod.service.component.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

/**
 * @Author Jaylin
 * @Date 2018-04-24
 * @Descript 资金冻结订单服务
 */
@Service
public class FundsUnfreezeOrderService {

    @Autowired
    private FundsUnfreezeOrderMapperExt unfreezeOrderMapperExt;
    @Autowired
    private SnowflakeIdWorker idWorker;
    @Autowired
    private LanMaoSequence lanMaoSequence;
    @Autowired
    private AccountService accountService;

    /**
     * 解冻申请订单
     *
     * @param applyRequestDTO
     * @return
     */
    @Transactional
    public FundsUnfreezeOrder applyUnfreezeOrder(FundsUnfreezeOrderApplyRequestDTO applyRequestDTO) {
        AccountBase accountBase = accountService.loadAccountById(applyRequestDTO.getAccountId());
        FundsUnfreezeOrder unfreezeOrder = new FundsUnfreezeOrder();
        String requestNo = lanMaoSequence.getRequestNo();
        unfreezeOrder.setId(idWorker.nextId());
        unfreezeOrder.setRequestNo(requestNo);
        unfreezeOrder.setOrderId(applyRequestDTO.getOrderId());
        unfreezeOrder.setOrderType(applyRequestDTO.getFundsOrderTypeEnum().getType());
        unfreezeOrder.setFreezeRequestNo(applyRequestDTO.getFreezeRequestNo());
        unfreezeOrder.setAccountId(applyRequestDTO.getAccountId());
        unfreezeOrder.setUserId(accountBase.getUserId());
        unfreezeOrder.setPlatformUserNo(accountBase.getAccountNo());
        unfreezeOrder.setAmount(applyRequestDTO.getAmount());
        unfreezeOrder.setStatus(FundsFreezeOrderStatusEnum.STATUS_DEAL.getStatus());
        unfreezeOrder.setCreateTime(new Date());
        unfreezeOrder.setUpdateTime(new Date());
        unfreezeOrderMapperExt.insertSelective(unfreezeOrder);
        return unfreezeOrder;
    }

    /**
     * 更新解冻订单成功状态==成功
     *
     * @param id
     */
    @Transactional
    public void updateUnfreezeOrderSuccess(Long id) {
        FundsUnfreezeOrder unfreezeOrder = new FundsUnfreezeOrder();
        unfreezeOrder.setId(id);
        unfreezeOrder.setStatus(FundsFreezeOrderStatusEnum.STATUS_SUCCESS.getStatus());
        unfreezeOrder.setTransactionTime(new Date());
        unfreezeOrder.setUpdateTime(new Date());

        unfreezeOrderMapperExt.updateByPrimaryKeySelective(unfreezeOrder);
    }

    /**
     * 更新解冻订单成功状态==失败
     *
     * @param id
     * @param errorCode
     * @param errorMsg
     */
    @Transactional
    public void updateUnfreezeOrderFail(Long id,String errorCode,String errorMsg) {
        FundsUnfreezeOrder unfreezeOrder = new FundsUnfreezeOrder();
        unfreezeOrder.setId(id);
        unfreezeOrder.setStatus(FundsFreezeOrderStatusEnum.STATUS_FAIL.getStatus());
        unfreezeOrder.setErrorCode(errorCode);
        unfreezeOrder.setErrorMessage(errorMsg);
        unfreezeOrder.setUpdateTime(new Date());

        unfreezeOrderMapperExt.updateByPrimaryKeySelective(unfreezeOrder);
    }

    public FundsUnfreezeOrder selectByPrimaryKeyForUpdate(Long id) {
        return unfreezeOrderMapperExt.selectByPrimaryKeyForUpdate(id);
    }

    public FundsUnfreezeOrder selectByPrimaryKey(Long id) {
        return unfreezeOrderMapperExt.selectByPrimaryKey(id);
    }

    public FundsUnfreezeOrder queryUnfreezeOrderByRequestNo(String requestNo) {
        return unfreezeOrderMapperExt.queryUnfreezeOrderByRequestNo(requestNo);
    }

    /**
     * 根据订单id查询解冻记录
     *
     * @param orderId
     * @param orderType
     * @return
     */
    public FundsUnfreezeOrder queryUnfreezeOrderByOrderId(Long orderId, Byte orderType) {
        FundsUnfreezeOrder fundsUnfreezeOrder = unfreezeOrderMapperExt.queryUnfreezeOrderByOrderId(orderId, orderType);
        return fundsUnfreezeOrder;
    }

    /**
     * 查询处理中的冻结订单列表
     *
     * @return
     */
    public List<FundsUnfreezeOrder> listUnfreezeOrderProcessing() {
        return unfreezeOrderMapperExt.listUnfreezeOrderList(FundsFreezeOrderStatusEnum.STATUS_DEAL.getStatus());
    }
}
